<?php
namespace app\admin\middleware;
use app\common\Apiv;
use think\facade\Env;
/**
 * @author KEL
 * @参数，加密验证 
 * @Date   2020-07-09 17:34:42
 * @Author  <1067547248@qq.com>
 */
class Auth{
	public function handle($request, \Closure $next){
		//获取appid
		$appid = input('appid','');
		$time  = input('time', '');
		$sign  = input('sign', '');
		//从.env文件获取配置
		$appsecret = Env::get('APP_SECRET');
		//基本参数验证
		if(!$appid){
			return Apiv::result(0,1000);
		}
		if(!$sign){
			return Apiv::result(0,1000);
		}
		if(!$time){
			return Apiv::result(0,1002);
		}
		//允许前后10秒的时间差,可根据实际情况调整
		if(time()<$time-10){
			return Apiv::result(0,1005);
		}
		if(time()>=$time+10){
			return Apiv::result(0,1006);
		}
		
		//简单签名验证
		$ckSign = md5($appid.$appsecret.$time);
		if($sign!=$ckSign){
			return Apiv::result(0,1004);
		}
		return $next($request);
    }


    public function end(\think\Response $response){
        // 回调行为
        // return $response;
    }
}